﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

public partial class adminstrator_selCourse : System.Web.UI.Page
{
    public OleDbConnection myConnection;
    public static readonly string sconnectionstring = System.Configuration.ConfigurationManager.AppSettings["SQLConnString"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        myConnection = new OleDbConnection(sconnectionstring);
        myConnection.Open();
        if (Session["UserName"] == null && Session["UserNo"] == null)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
                "alert('登陆超时(或未登录),请您重新登陆！'),parent.location.href='..index.aspx/'</script>", false);
        }
        
            bind1();
            bind2();
        
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bind1();
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string classNo = GridView1.DataKeys[e.NewSelectedIndex][0].ToString();
        string classrs = GridView1.DataKeys[e.NewSelectedIndex][1].ToString();
        string classname= GridView1.DataKeys[e.NewSelectedIndex][2].ToString();
        string classjsyt = GridView1.DataKeys[e.NewSelectedIndex][3].ToString();
        string TeacherNo = GridView1.DataKeys[e.NewSelectedIndex][4].ToString();
        string classdate = GridView1.DataKeys[e.NewSelectedIndex][5].ToString();
        string myinsert = "insert into UsqbTb (sno,sclass,sjsyt,sxq,srs,tell,sdmt,ApplyResult,rNo,aDtate,aNo,rDate) values ('" + Session["UserNo"].ToString() + "','" + Session["UserName"].ToString() + "','" + 
            classNo + "','" + classrs + "','" + classname + "','" + classjsyt + "','" + TeacherNo + "','" + classdate + "') ";
        OleDbCommand mycmd = new OleDbCommand(myinsert, myConnection);
        mycmd.ExecuteScalar();
        myinsert = "insert into findplan (sno,sclass,sdmt,aDate,aNo,rDate) values ('" + Session["UserNo"].ToString() + "','" + Session["UserName"].ToString() + "','" +
            classNo + "','" + classrs + "','" + TeacherNo + "','" + classdate + "') ";
        mycmd = new OleDbCommand(myinsert, myConnection);
        mycmd.ExecuteScalar();
        bind1();
        bind2();
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView2.PageIndex = e.NewPageIndex;
        bind2();
    }
    protected void GridView2_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string courseNo = GridView2.DataKeys[e.NewSelectedIndex][0].ToString();
        string mysql = "select * from findplan where sno='" + Session["UserNo"].ToString() + "' and aNo='" + courseNo + "'  ";
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataReader myreader = mycmd.ExecuteReader();
        if (myreader.Read())
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
               "alert('不允许取消！');</script>", false);
        else
        {
            string mystr = "delete from UsqbTb where sno='" + Session["UserNo"].ToString() + "' and aNo='" + courseNo + "' ";
            mycmd = new OleDbCommand(mystr, myConnection);
            mycmd.ExecuteNonQuery();
            mystr = "delete from findplan where sno='" + Session["UserNo"].ToString() + "' and aNo='" + courseNo + "' ";
            mycmd = new OleDbCommand(mystr, myConnection);
            mycmd.ExecuteNonQuery();
            bind1();
            bind2();
        }
    }

    public void bind1()
    {
        string mysql = "select * from RoomTB where rNo not in (select rNo from UsqbTb where sno='" + Session["UserNo"].ToString() + "') order by ID ";
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataAdapter Adapter = new OleDbDataAdapter();
        Adapter.SelectCommand = mycmd;
        DataSet myDS = new DataSet();
        Adapter.Fill(myDS);
        if (myDS.Tables.Count > 0 && myDS.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = myDS;
            GridView1.DataKeyNames = new string[] { "rNo", "srs", "sclass", "sjsyt", "aDate", "rDate" };
            GridView1.DataBind();
            label1.Text = "";
        }
        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();

        }
    }
    public void bind2()
    {
        string mysql = "select * from UsqbTb where sno='" + Session["UserNo"].ToString() + "' order by ID ";
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataAdapter Adapter = new OleDbDataAdapter();
        Adapter.SelectCommand = mycmd;
        DataSet myDS = new DataSet();
        Adapter.Fill(myDS);
        if (myDS.Tables.Count > 0 && myDS.Tables[0].Rows.Count > 0)
        {
            GridView2.DataSource = myDS;
            GridView2.DataKeyNames = new string[] { "sjsyt" };
            GridView2.DataBind();
            label2.Text = "";
        }
        else
        {
            GridView2.DataSource = null;
            GridView2.DataBind();

        }
    }
}